package com.yanqu.road.server.cmd.activity.silkroad;

import com.yanqu.road.dao.impl.player.UserSilkRoadDaoImpl;
import com.yanqu.road.entity.activity.silkroad.UserSilkRoadData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SilkRoadProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.silkroad.SilkRoadModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Cmd(code = GameProtocol.S_SILK_ROAD_LOOK_OTHER_RELIC, desc = "查看其他玩家的博物")
public class SilkRoadLookOtherReflicCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {

        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SilkRoadProto.SilkRoadLookOtherRelicReqMsg reqMsg = SilkRoadProto.SilkRoadLookOtherRelicReqMsg.parseFrom(packet.getBytes());
        SilkRoadProto.SildRoadLookOtherRelicRespMsg.Builder resp = SilkRoadProto.SildRoadLookOtherRelicRespMsg.newBuilder();
        resp.setRet(0);
        if (reqMsg.getServerId() == GameServer.getInstance().getServerId()) {
            GamePlayer targetPlayer = GamePlayerMgr.getOnlinePlayer(reqMsg.getUserId());
            UserSilkRoadData userData = null;
            if (targetPlayer == null) {
                userData = new UserSilkRoadDaoImpl().getUserSilkRoadData(reqMsg.getUserId(), reqMsg.getActivityId());
            } else {
                userData = targetPlayer.getModule(SilkRoadModule.class).getUserData();
            }
            if (userData != null) {
                resp.setAchievement(userData.getAchievement());
                for (Map.Entry<Integer, Integer> entry : userData.getRefloatItemMap().entrySet()) {
                    SilkRoadProto.SilkRoadRelicTemp.Builder relicsBuilder = SilkRoadProto.SilkRoadRelicTemp.newBuilder();
                    relicsBuilder.setRelicId(entry.getKey());
                    relicsBuilder.setNum(entry.getValue());

                    resp.addRelics(relicsBuilder);
                }
            } else {
                resp.setRet(GameErrorCode.E_ERROR_DATA);
            }
            player.sendPacket(ClientProtocol.U_SILK_ROAD_LOOK_OTHER_RELIC, resp);

        } else {
            CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToGameServer(reqMsg.getServerId(), GameProtocol.S_SILK_ROAD_LOOK_OTHER_RELIC, reqMsg.getUserId(), reqMsg.toBuilder(), resp);
            future.thenAccept((SyncResponse syncResponse) -> {
                if (syncResponse.getErrorCode() == 0) {
                    player.sendPacket(ClientProtocol.U_SILK_ROAD_LOOK_OTHER_RELIC, resp);
                } else {
                    player.sendPacket(ClientProtocol.U_SILK_ROAD_LOOK_OTHER_RELIC, SilkRoadProto.SildRoadLookOtherRelicRespMsg.newBuilder().setRet(syncResponse.getErrorCode()));
                }
            });
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            player.sendPacket(ClientProtocol.U_SILK_ROAD_LOOK_OTHER_RELIC, SilkRoadProto.SildRoadLookOtherRelicRespMsg.newBuilder().setRet(ret));
        }
    }
}
